
var myScroll,
	pullDownEl, pullDownOffset,
	pullUpEl, pullUpOffset,
	generatedCount = 0;
var Goble_Can_Updata = true;
var Goble_NoData = false;
var Step = 0;
var EndPos = 10;
function pullDownAction () {
	pullUpAction();
}


window.onload = function () {
	pullDownAction();

};
function pullUpAction () {
	//判断是否可以提交
	if(!Goble_Can_Updata){
		return 0;
	}
	//这里ajax 拖数据
	$.ajax({
		  url: "/f/xuqiu/datagrid",
		  type: "POST",
		  dataType: "json",
		  data: {
		  	type:'pulldown',
		  	Curtime:Curtime,
		  	Step:Step,
		  	EndPos:EndPos,
		  	tajax:tajax
		  },
		  //触发ajax请求开始时执行
		  beforeSend:function(){
				Goble_Can_Updata = false;
		  },
		  complete:function(){
				Goble_Can_Updata = true;
		  },
		  success: function(data){
		  	if(data.re=="good"){
		  		if(data.HasData==0){
		  			//证明没有更多数据了 
		  			$('.pullUpIcon').hide();
		  			Goble_NoData = true;
		  		}else
		  			$(data.ayi_lie_List).appendTo('#thelist');
		  			//Refresch_BtFun();
		  		EndPos = data.EndPos;		  		
		  	}
		  	//setTimeout(myScroll.refresh(), 1000);
		    myScroll.refresh();
		  },
		  error: function(XMLHttpRequest, textStatus, errorThrown) {
              alert(XMLHttpRequest.status);
              alert(XMLHttpRequest.readyState);
              alert(textStatus);
              alert(XMLHttpRequest.responseText);
   		  }
	});
}

//这里设置一个全局的是否方法和缩小的控制
var G_Small_Con = 0;			//0 为缩小状态 	1为放大状态
var G_SetPosX = 0;

function loaded() {
	//首先先 重置 wrapper 的位置
	var SetPosX = $('.bgk_title').position().top+28;
	SetPosX+= $('.bgk_title').height();
	G_SetPosX = SetPosX;

	$('#wrapper').css("top",SetPosX);
	
	pullDownEl = document.getElementById('pullDown');
	pullDownOffset = pullDownEl.offsetHeight;
	pullUpEl = document.getElementById('pullUp');	
	pullUpOffset = pullUpEl.offsetHeight;
	
	if(EndPos==0){
		pullUpEl.className = '';
		pullUpEl.querySelector('.pullUpLabel').innerHTML = '暂时没有数据&nbsp;&nbsp;&nbsp;';
		return ;
	}
	
	myScroll = new iScroll('wrapper', {
		useTransition: true,
		topOffset: pullDownOffset,
		onRefresh: function () {
			if (pullDownEl.className.match('loading')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '向下拖动刷新...';
			} else if (pullUpEl.className.match('loading')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '向上拖动获得更多...';
			}
			if(Goble_NoData){
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '已经没有更多数据&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			}
		},
		onScrollMove: function () {

			if (this.y > 5 && !pullDownEl.className.match('flip')) {
				pullDownEl.className = 'flip';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '释放刷新...';
				this.minScrollY = 0;
			} else if (this.y < 5 && pullDownEl.className.match('flip')) {
				pullDownEl.className = '';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '向下拖动刷新...';
				this.minScrollY = -pullDownOffset;
			} 
			if(Goble_NoData){
				return ;
			}

			if (this.y < (this.maxScrollY - 5) && !pullUpEl.className.match('flip')) {
				pullUpEl.className = 'flip';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '释放刷新...';
				this.maxScrollY = this.maxScrollY;
			} else if (this.y > (this.maxScrollY + 5) && pullUpEl.className.match('flip')) {
				pullUpEl.className = '';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '向上拖动获得更多...';
				this.maxScrollY = pullUpOffset;
			}
			
			/*
			//这里进行缩放
			if(this.y < -88 && G_Small_Con==0)
			{
				//此为最上面的logo栏 以及logo图片的缩小
				$(".head_Top").css({"height":"2em","line-height":"2em","transition":"all 1s"});
				$(".head_Top img").css({"height":"2em","transition":"all 1s"});
				//此为订单类型的选择的向上移动
				$(".bgk_checo").css({"top":"3.4em","transition":"all 1s"});
				
				//此为订单整体列表的上移动
				var Tmpposy = G_SetPosX-28;
				$("#wrapper").css({"top":Tmpposy,"transition":"all 1s"});
				G_Small_Con =1;
				
				//console.log("缩小："+this.y);
				//console.log("缩小："+Tmpposy);
			}
			//放大
			if(this.y > -58 && G_Small_Con==1)
			{
				//此为最上面的logo栏 以及logo图片的缩小
				$(".head_Top").css({"height":"3em","line-height":"3em","transition":"all 1s"});
				$(".head_Top img").css({"height":"3em","transition":"all 1s"});
				//此为订单类型的选择的向上移动
				$(".bgk_checo").css({"top":"5.4em","transition":"all 1s"});
				
				//此为订单整体列表的上移动
				var Tmpposy = G_SetPosX;
				$("#wrapper").css({"top":Tmpposy,"transition":"all 1s"});
				G_Small_Con = 0;
				
				//console.log("缩放大："+this.y);
				//console.log("缩放大："+Tmpposy);
			}
			//console.log("中间："+this.y);
			*/
		},
		onScrollEnd: function () {
			if (pullDownEl.className.match('flip')) {
				pullDownEl.className = 'loading';
				pullDownEl.querySelector('.pullDownLabel').innerHTML = '加载中...';				
				pullDownAction();	// Execute custom function (ajax call?)
			} 
			if(Goble_NoData){
				return ;
			}
			if (pullUpEl.className.match('flip')) {
				pullUpEl.className = 'loading';
				pullUpEl.querySelector('.pullUpLabel').innerHTML = '加载中...';				
				pullUpAction();		// Execute custom function (ajax call?)
			}
		}
	});
	
	setTimeout(Set_wrapper, 800);
	//setTimeout(function () { document.getElementById('wrapper').style.left = '0'; }, 800);
}

function Set_wrapper(){
	//首先 获得 width 
	var wtidhtmp = $('body').width()/2-$('.One_DingDanShow').outerWidth()/2-12;
	//document.getElementById('wrapper').style.left = wtidhtmp;
	$('#wrapper').css('left',wtidhtmp);
	//alert(wtidhtmp);
}

//document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

document.addEventListener('DOMContentLoaded', function () { setTimeout(loaded, 800); }, false);


function refrwshh_fun(){
	//如果下面两个参数相差 超过40 就证明出现问题了 就要调整了
	if(myScroll.scroller.clientHeight-myScroll.scrollerH>100){
		myScroll.refresh();
	}	
}

//setInterval(refrwshh_fun,1000); 